<template>
  <a-drawer
    :title="title"
    :width="width"
    placement="right"
    :closable="false"
    @close="close"
    :visible="visible">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

<!--        <a-form-model-item label="所属基地" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
<!--          <a-select  v-decorator="['sourceBase', validatorRules.sourceBase]" placeholder="请选择所属基地" @change="handleChange">-->
<!--            <a-select-option v-for="d in baseList" :key="d.id" :value="d.id">{{ d.name }}</a-select-option>-->
<!--          </a-select>-->
<!--        </a-form-model-item>-->
        <a-form-model-item label="产地" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select  v-decorator="['sourceGreenhouse', validatorRules.sourceGreenhouse]" placeholder="请选择产地" >
            <a-select-option v-for="d in greenhouseList" :key="d.id" :value="d.id">{{ d.name }}</a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-item label="采收产品" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['variety', validatorRules.variety]" :trigger-change="true" dictCode="planting_variety" placeholder="请选择采收产品"/>
        </a-form-item>
        <a-form-item label="采收分类" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['classify', validatorRules.classify]" :trigger-change="true" dictCode="planting_classify" placeholder="请选择采收分类"/>
        </a-form-item>
        <a-form-item label="分拣员" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['sorter', validatorRules.sorter]" :trigger-change="true" dictCode="planting_sorter" placeholder="请选择分拣员"/>
        </a-form-item>
        <a-form-item label="包装员" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['packer', validatorRules.packer]" placeholder="请输入包装员"></a-input>
        </a-form-item>
        <a-form-item label="分拣日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择分拣日期" v-decorator="['sortTime', validatorRules.sortTime]" :trigger-change="true" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="打包规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['packingSpecifications', validatorRules.packingSpecifications]" :trigger-change="true" dictCode="planting_packing_specifications" placeholder="请选择打包规格"/>
        </a-form-item>
        <a-form-item label="标签数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['tagNum', validatorRules.tagNum]" placeholder="请输入标签数量" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="分拣数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['sortNum', validatorRules.sortNum]" placeholder="请输入分拣数量" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['flowerPoleLength', validatorRules.flowerPoleLength]" :trigger-change="true" dictCode="planting_flower_pole" placeholder="请选择单位"/>
        </a-form-item>

      </a-form>
    </a-spin>
    <a-button type="primary" @click="handleOk">确定</a-button>
    <a-button type="primary" @click="handleCancel">取消</a-button>
  </a-drawer>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import { queryFlowerBase, queryTodayData, queryGreenhouseByPd } from '@/api/api'

  export default {
    name: "FlowerTagInfoModal",
    components: {
      JDate,
      JDictSelectTag,
    },
    data () {
      return {
        baseList: [], //基地信息
        greenhouseList: [], //大棚信息
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          // sourceBase: {
          //   rules: [
          //     { required: true, message: '请输入基地!'},
          //   ]
          // },
          sourceGreenhouse: {
            rules: [
              { required: true, message: '请选择产地!'},
            ]
          },
          variety: {
            rules: [
              { required: true, message: '请输入采收产品!'},
            ]
          },
          classify: {
            rules: [
              { required: true, message: '请输入采收分类!'},
            ]
          },
          sorter: {
            rules: [
              { required: true, message: '请输入分拣员!'},
            ]
          },
          packer: {
            rules: [
              { required: true, message: '请输入包装员!'},
            ]
          },
          sortTime: {
            rules: [
              { required: true, message: '请输入分拣日期!'},
            ]
          },
          packingSpecifications: {
            rules: [
              { required: true, message: '请输入打包规格!'},
            ]
          },
          tagNum: {
            rules: [
              { required: true, message: '请输入标签数量!'},
            ]
          },
          sortNum: {
            rules: [
              { required: true, message: '请输入分拣数量!'},
            ]
          },
          flowerPoleLength: {
            rules: [
              { required: true, message: '请输入单位!'},
            ]
          },
        },
        url: {
          add: "/flower/tagInfo/addPc",
          edit: "/flower/tagInfo/edit",
        }
      }
    },
    created () {
      this.initBaseInfo()
      this.initGreenhouseInfo()
    },
    methods: {
      handleChange(value) {
        console.log(value)
        queryGreenhouseByPd(value).then((res)=>{
          if(res.success){
            this.greenhouseList = res.result;
          }
        });
      },
      //初始化基地信息
      initBaseInfo(){
        queryFlowerBase().then((res)=>{
          if(res.success){
            this.baseList = res.result;
          }
        });
      },
      initGreenhouseInfo(){
        queryTodayData().then((res)=>{
          if(res.success){
            this.greenhouseList = res.result;
          }
        });
      },
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        // this.handleChange(record.sourceBase)
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'sourceBase','sourceGreenhouse','variety','classify','sorter','packer','sortTime','packingSpecifications','tagNum','sortNum','flowerPoleLength'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }

            for (let key in this.greenhouseList) {
              if(!this.greenhouseList.hasOwnProperty(key)) continue;
              if (this.greenhouseList[key].id === values.sourceGreenhouse) {
                this.model.sourceBase = this.greenhouseList[key].name
                break
              }
            }

            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'sourceBase','sourceGreenhouse','variety','classify','sorter','packer','sortTime','packingSpecifications','tagNum','sortNum','flowerPoleLength'))
      }

    }
  }
</script>

<style lang="less" scoped>
/** Button按钮间距 */
  .ant-btn {
    margin-left: 30px;
    margin-bottom: 30px;
    float: right;
  }
</style>